﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.数据查询
{
    /// <summary>
    /// 数据查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class DataQueryController : BaseController
    {
        private readonly IInstructService instructService;
        private readonly IInterfaceLogService interlogService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="instructService"></param>
        /// <param name="interlogService"></param>
        public DataQueryController(IWebHostEnvironment environment, IInstructService instructService, IInterfaceLogService interlogService) : base(environment)
        {
            this.instructService = instructService;
            this.interlogService = interlogService;
        }

        /// <summary>
        /// 获取指令列表
        /// </summary>
        /// <param name="InstructType"></param>
        /// <param name="Status"></param>
        /// <param name="PalletBarcode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInstructList(int InstructType = 0, int Status = 0, string? PalletBarcode = "", int pageIndex = 1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = instructService.GetInstructList(InstructType, Status, PalletBarcode, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 指令-货品明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInstructMater(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = instructService.GetInstructMater(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// WCS接口日志
        /// </summary>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInterfaceLogList(int? status, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = interlogService.GetInterfaceLogList(status, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
        }

        /// <summary>
        /// 撤销指令
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult CancelInstruct(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message = "";
            bool result = instructService.CancelInstruct(id, out message);
            if (result)
                return Ok(BaseResult.Result(true, "撤销成功！", "200"));
            return Ok(BaseResult.Result(false, "删除失败！" + message, "500"));
        }
    }
}
